#include "removablemediascanner.h"
#include <QDir>

RemovableMediaScanner::RemovableMediaScanner()
{
    drivesList=new QStringList();
}

void RemovableMediaScanner::run() {
    while(true) {
        QStringList * drives=new QStringList();
        #if defined(Q_OS_WIN)
            foreach(QFileInfo info, QDir::drives()) {
                if(info.absolutePath()!=QDir::rootPath()) {
                    drives->append(info.absolutePath());
                }
            }
        #else
            #if defined(Q_OS_MAC)
                QDir driveDir("/Volumes");
            #else
                QDir driveDir("/mount");
            #endif
            foreach(QFileInfo drive,driveDir.entryInfoList(QDir::AllDirs)) {
                if(drive.canonicalPath()!=QDir::rootPath()) {
                    if(drive.fileName()!="."&&drive.fileName()!="..") {
                        drives->append(drive.absolutePath());
                    }
                }
            }
        #endif

        foreach(QString drive, *drives) {
            if(!drivesList->contains(drive)) {
                emit driveAdded(drive);
            }
        }

        foreach(QString drive, *drivesList) {
            if(!drives->contains(drive)) {
                emit driveRemoved(drive);
            }
        }

        delete drivesList;
        drivesList=drives;
        sleep(2*60*1000/3);
    }
}
